package com.example.farmeri.presenter.farm_data;

import android.util.Log;

import com.example.farmeri.http.API;
import com.example.farmeri.pojo.FarmDataT;
import com.example.farmeri.presenter.base.BasePresenter;
import com.example.farmeri.tools.ResponseUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class FarmDataLookPresenter extends BasePresenter<IFarmDataLookContract> {


    public FarmDataLookPresenter(IFarmDataLookContract view) {
        super(view);
    }


    /**
     * 获取近{count}天的日期
     * @param count
     * @return 日期组成的集合
     */
    public Map<Integer, String>  getRecentDate(Integer count){
        String [] arr = new String[count];
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        Calendar c = null;
        for (int i=0;i<count;i++){
            c=Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, - i-1);
            arr[count-1-i] =sdf.format(c.getTime());
        }


        Map<Integer, String> map = new HashMap<>();
        map.put(0, "");
        map.put(count+1, "");
        for (int i = 1; i <= count; i++) {
            map.put(i, arr[i-1]);
        }
        return map;
    }


    /**
     * 获取温度、湿度、光照每日数据
     * @return
     * @param data
     */
    public Map<Integer,Float> getData(String[] data) {
        Map<Integer,Float> map = new HashMap<>();
        map.put(0, 0f);
        map.put(data.length+1, 0f);
        for (int i = 1; i <= data.length; i++) {
            map.put(i, Float.parseFloat (data[i-1]));
        }
        return map;
    }


    public void getFarmData(String farmId, String beginDate, String endDate) {
        getRetrofit()
                .create(API.class)
                .getFarmData(farmId,beginDate,endDate)
                .observeOn(AndroidSchedulers.mainThread())
//                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils<FarmDataT>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils<FarmDataT> roleResponseUtils) {
                        if (roleResponseUtils.getCode() == 200){
                            getView().onFarmDataResult(roleResponseUtils.getData());
                        }else {
                            getView().onFarmDataError(roleResponseUtils.getMessage());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e(TAG, "onError: e==>" + e.getMessage());
                        getView().onFarmDataError("数据获取失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
